<template>
	<view class="m-input-view">
		<!-- <input :disabled="disabled" :focus="focus_" :type="inputType" :value="value" @input="onInput" class="m-input-input" :placeholder="placeholder"
		 :password="type==='password'&&!showPassword" @focus="onFocus" @blur="onBlur" />
		 -->
		<input :disabled="disabled" :class="[disabled?'gray':'']"  type="text" :value="value" @input="onInput" class="m-input-input" :placeholder="placeholder"
		 :password="type==='password'&&!showPassword" @focus="onFocus" @blur="onBlur" />
		
		<!-- 优先显示密码可见按钮 -->
		<view v-if="clearable_&&!displayable_&&value.length&&!disabled" class="m-input-icon">
			<text  class="cuIcon-roundclose text-gray" @click="clear"></text>
		</view>
		<view v-if="displayable_&&!disabled" class="m-input-icon">
			<text  :class="showPassword?'cuIcon-attentionfavor' :'cuIcon-attentionforbid'" @click="display"></text>
		</view>
	</view>
</template>

<script>

	export default {
		props: {
			type: String,
			value: String,
			placeholder: String,
			/*是否显示清除按钮*/
			clearable: {
				type: [Boolean, String],
				default: false
			},
			/* 是否显示密码可见按钮*/
			displayable: {
				type: [Boolean, String],
				default: false
			},
			/* 自动获取焦点*/
			focus: {
				type: [Boolean, String],
				default: false
			},
			disabled:{
				type: Boolean,
				default: false
			},
		},
		model: {
			prop: 'value',
			event: 'input'
		},
		data() {
			return {
				//显示密码明文
				showPassword: false,
				// 是否获取焦点
				isFocus: false,
			}
		},
		computed: {
			inputType() {
				const type = this.type || 'text';
				return type === 'password' ? 'text' : type
			},
			clearable_() {
				return String(this.clearable) !== 'false'
			},
			displayable_() {
				return String(this.displayable) !== 'false'
			},
			focus_() {
				return String(this.focus) !== 'false'
			}
		},
		methods: {
			clear() {
				this.$emit('input', '')
			},
			display() {
				this.showPassword = !this.showPassword
			},
			onFocus() {
				this.isFocus = true
			},
			onBlur() {
				this.$nextTick(() => {
					this.isFocus = false
				})
			},
			onInput(e) {
				this.$emit('input', e.target.value)
			}
		}
	}
</script>

<style>
	.m-input-icon {
		width: 20px;
		position: absolute;
		right: 0;
	}
	.gray{
		/* background-color: A2A8A2; */
	}
</style>
